<template>
  <div>
    <el-card class="box-card" shadow="hover">
      <Pagination ref="pagination" :queryPage="loadPage">
        <div slot="search">
          <el-form :inline="true" :model="queryForm" size="mini">
            <el-form-item label="应用名称">
              <el-select v-model="queryForm.appName" placeholder="名称">
                <el-option v-for="app in appList" :key="app.id" :label="app.appShowName" :value="app.appName"/>
              </el-select>
            </el-form-item>
            <el-form-item label="库名">
              <el-input v-model="queryForm.name" placeholder="名称"/>
            </el-form-item>
            <el-form-item label="主机">
              <el-input v-model="queryForm.host" placeholder="主机"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="$refs.pagination.go(1);">查询</el-button>
              <el-button type="success" @click="onEdit({})">新增
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table slot="table" :data="pageData" border size="mini" style="width: 100%">
          <el-table-column label="#ID" prop="id" width="80"/>
          <el-table-column label="库名" prop="name" show-overflow-tooltip width="150"/>
          <el-table-column label="应用名称" prop="appName" show-overflow-tooltip width="120"/>
          <el-table-column label="类型" prop="type.name" width="80"/>
          <el-table-column label="环境类型" prop="envType.name" width="80"/>
          <el-table-column label="主机" prop="host" width="120"/>
          <el-table-column label="端口" prop="port" width="60"/>
          <el-table-column label="创建时间" prop="created" width="150"/>
          <el-table-column label="更新时间" prop="updated" width="150"/>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button size="small" type="text" @click="onEdit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </Pagination>
    </el-card>

    <el-dialog :visible.sync="editVisible" title="主机设置" width="600px">
      <el-form :model="editData" label-width="80px" size="mini" style="width: 400px;margin-left: 50px">
        <el-form-item label="应用名称">
          <el-select v-model="editData.appName" :disabled="editData.id!=null"
                     default-first-option style="width: 100%">
            <el-option v-for="app in appList" :key="app.id" :label="app.appShowName" :value="app.appName"/>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="editData.type" :disabled="editData.id!=null"
                     default-first-option style="width: 100%">
            <el-option v-for="databaseType in databaseTypes" :key="databaseType.code" :label="databaseType.name"
                       :value="databaseType.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="环境">
          <el-select v-model="editData.envType" :disabled="editData.id!=null"
                     default-first-option style="width: 100%">
            <el-option v-for="envType in envTypes" :key="envType.code" :label="envType.name" :value="envType.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="库名">
          <el-input v-model="editData.name"/>
        </el-form-item>
        <el-form-item label="主机">
          <el-input v-model="editData.host"/>
        </el-form-item>
        <el-form-item label="端口">
          <el-input v-model="editData.port"/>
        </el-form-item>

        <el-form-item label="用户名">
          <el-input v-model="editData.username"/>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="editData.password"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSave">保存</el-button>
          <el-button :loading="testResult=='info'" :type="testResult" @click="onTest">测试</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {appListApi, databaseInstancePageApi, deployGroupListApi, saveDatabaseInstanceApi} from "@/api/manage";
import Pagination from "@/components/Pagination";
import {enumsApi} from "@/api/common";
import {execTestApi} from "@/api/database";

export default {
  data() {
    return {
      queryForm: {},
      pageData: [],
      editData: {},
      editVisible: false,
      appList: [],
      envTypes: [],
      databaseTypes: [],
      testResult: "success",
    };
  },
  components: {Pagination},
  methods: {
    async loadPage(current, size) {
      let query = Object.assign({current: current, size: size,}, this.queryForm);
      let data = await databaseInstancePageApi(query);
      this.pageData = data.records;
      return data;
    },
    onEdit(data) {
      this.editData = Object.assign({}, data);
      if (data.type) {
        this.editData.type = data.type.code;
      }
      if (data.envType) {
        this.editData.envType = data.envType.code;
      }
      this.testResult = 'success'
      this.editVisible = true;
    },
    async onSave() {
      await saveDatabaseInstanceApi(this.editData);
      this.$message({type: "success", message: "保存成功!"});
      this.editVisible = false;
      this.$refs.pagination.go(1);
    },
    async appNameChange() {
      this.groupList = await deployGroupListApi(this.editData.appName);
      this.editData.deployGroupName = null;
    },
    groupChange(group) {
      console.log(group)
      let gs = this.groupList.filter(g => g.name == group);
      if (gs.length == 1) {
        this.editData.envType = gs[0].envType.code;
      }
      this.$forceUpdate()
    },
    async onTest() {
      this.testResult = 'info'
      let r = await execTestApi(this.editData);
      if (r) {
        this.testResult = 'success';
        this.$message({type: "success", message: "测试连接成功!"});
      } else {
        this.testResult = 'danger';
        this.$message({type: "error", message: "测试连接失败!"});
      }

    }
  },
  async created() {
    this.appList = await appListApi();
    this.envTypes = await enumsApi("EnvType");
    this.databaseTypes = await enumsApi("DatabaseType");
  }
};
</script>